(function()
{
var dav, ns, nssp, qn, qna, an, list, tests = {
    
    
    /**
     *    XML settings tests
     *
     *
     */
    test_setSettings_to_defaultSettings: function ()
    {
        var d = XML.defaultSettings(), p;
        
        XML.setSettings(d);
        
        for (p in d)
        {
            if (XML[p] !== d[p])
            {
                return false;
            }
        }
        
        return true;
    },
    
    test_compare_settings_to_defaultSettings: function ()
    {
        var d = XML.defaultSettings(), s = XML.settings(), p;
        
        for (p in d)
        {
            if (s[p] !== d[p])
            {
                return false;
            }
        }
        
        return true;
    },
    
    
    /**
     *    Supplemental Object tests
     *
     *
     */
    test_setDefaultNamespace_using_namespace: function ()
    {
        setDefaultNamespace(new Namespace("urn://string-set-default-namespace"));
        
        var x = XML("<xml/>");
        return x.namespace().uri === "urn://string-set-default-namespace"
                && x.name().uri === "urn://string-set-default-namespace";
    },
    
    test_setDefaultNamespace_using_string: function ()
    {
        setDefaultNamespace("urn://string-set-default-namespace-attr");
        
        var x = XML("<xml/>");
        
        return x.namespace().uri === "urn://string-set-default-namespace-attr"
                && x.name().uri === "urn://string-set-default-namespace-attr";
    }, 
    
    test_Namespace: function ()
    {
        ns = new Namespace("D", "DAV:");
        
        return ns instanceof Namespace 
                && ns.uri == "DAV:" 
                && ns.prefix == "D";
    },
    
    test_Namespace_one_argument: function ()
    {
        var ns = new Namespace("DAV:");
        
        return ns instanceof Namespace 
                && ns.uri == "DAV:";
    },
    
    test_Namespace_zero_arguments: function ()
    {
        var ns = new Namespace();
        
        return ns instanceof Namespace 
                && ns.uri === "" 
                && ns.prefix === "";
    },
    
    test_Namespace_as_function: function ()
    {
        nssp = Namespace("test:uri2");
        
        return nssp instanceof Namespace 
                && nssp.uri === "test:uri2"; 
    },
    
    test_Namespace_as_function_from_Namespace: function ()
    {
        var test = Namespace(nssp);
        
        return test instanceof Namespace 
                && test.uri === nssp.uri
                && test.prefix === nssp.prefix; 
    },
    
    test_QName: function ()
    {
        qn = new QName(ns, "response");
        
        return qn instanceof QName
                && qn.uri === "DAV:"
                && qn.localName === "response"
                && qn.toString() === "DAV:::response";
    },
    
    test_QName_zero_arguments: function ()
    {
        var qn = new QName();
        
        return qn instanceof QName
                && qn.uri === "urn://string-set-default-namespace-attr"
                && qn.localName === "";
    },
    
    test_QName_one_argument: function ()
    {
        var qn = new QName("*");
        
        return qn instanceof QName
                && qn.localName === "*";
    },
    
    test_QName_two_arguments: function ()
    {
        var qn = new QName("DAV:", "*");
        
        return qn instanceof QName
                && qn.uri === "DAV:"
                && qn.localName === "*";
    },
    
    test_QName_two_arguments_using_Namespace: function ()
    {
        var qn = new QName(ns, "*");
        
        return qn instanceof QName
                && qn.uri === "DAV:"
                && qn.localName === "*";
    },
    
    test_QName_as_function: function ()
    {
        qna = QName(ns, "test");
        
        return qna instanceof QName
                && qna.localName === "test"
                && qna.uri ===  "DAV:";
    },
    
    test_QName_as_function_on_existing_QName: function ()
    {
        return QName(qn) === qn;
    },
    
    test_QName_GetNamespace: function ()
    {
        if (qn._GetNamespace)
        {
            var nsi = qn._GetNamespace();
            return nsi.uri === ns.uri
                    && nsi.prefix === ns.prefix;
        }
        
        return true;
    },
    
    test_QName_GetNamespace_with_argument: function ()
    {
        if (qna._GetNamespace)
        {
            var nsi = qna._GetNamespace({"D": ns});
            return nsi.uri === ns.uri
                    && nsi.prefix === ns.prefix;
        }
        
        return true;
    },
    
    test_AttributeName: function ()
    {
        an = new AttributeName(qna);
        
        return an instanceof AttributeName
                && (!an._Name || an._Name === qna);
    },
    
    test_AttributeName_as_function: function ()
    {
        return AttributeName(an) instanceof AttributeName
                && AttributeName(qn) instanceof QName;
    },
    
    test_isXMLName_AttributeName: function ()
    {
        return !!isXMLName(an);
    },
    
    test_isXMLName_QName: function ()
    {
        return !!isXMLName(qn);
    },
    
    test_isXMLName_Namespace: function ()
    {
        return !isXMLName(ns);
    },
    
    
    
    /**
     *    XML tests
     *
     *
     */
    test_XML_no_arguments: function ()
    {
        var x = new XML();
        
        return x instanceof XML
                && x.length() == 1
                && x.nodeKind() === "text";
    },
    
    test_XML_text_argument: function ()
    {
        var x = new XML("this is a test text node");
        
        return x instanceof XML
                && x.length() == 1
                && x.toString() == "this is a test text node";
    },
    
    test_XML_complex_xml: function ()
    {
        dav = new XML('<D:multistatus xmlns:D="DAV:" att="test"><D:response><D:href>/</D:href><D:propstat><D:prop><D:displayname>Files</D:displayname><D:getcontentlength>0</D:getcontentlength><D:getlastmodified>Thu, 30 Jul 2009 21:28:20 GMT</D:getlastmodified><D:resourcetype><D:collection/></D:resourcetype></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat><D:propstat><D:prop><D:name/><D:parentname/><D:href/><D:ishidden/><D:isreadonly/><D:getcontenttype/><D:contentclass/><D:getcontentlanguage/><D:creationdate/><D:lastaccessed/><D:iscollection/><D:isstructureddocument/><D:defaultdocument/><D:isroot/></D:prop><D:status>HTTP/1.1 404 Not Found</D:status></D:propstat></D:response><D:response><D:href>/Public/</D:href><D:propstat><D:prop><D:displayname>Public</D:displayname><D:getcontentlength>0</D:getcontentlength><D:getlastmodified>Fri, 24 Jul 2009 04:16:12 GMT</D:getlastmodified><D:resourcetype><D:collection/></D:resourcetype></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat><D:propstat><D:prop><D:name/><D:parentname/><D:href/><D:ishidden/><D:isreadonly/><D:getcontenttype/><D:contentclass/><D:getcontentlanguage/><D:creationdate/><D:lastaccessed/><D:iscollection/><D:isstructureddocument/><D:defaultdocument/><D:isroot/></D:prop><D:status>HTTP/1.1 404 Not Found</D:status></D:propstat></D:response><D:response><D:href>/SamShull_business_card.pdf</D:href><D:propstat><D:prop><D:displayname>SamShull_business_card.pdf</D:displayname><D:getcontentlength>44154</D:getcontentlength><D:getlastmodified>Fri, 24 Jul 2009 13:45:57 GMT</D:getlastmodified><D:getcontenttype>application/pdf</D:getcontenttype><D:resourcetype/></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat><D:propstat><D:prop><D:name/><D:parentname/><D:href/><D:ishidden/><D:isreadonly/><D:contentclass/><D:getcontentlanguage/><D:creationdate/><D:lastaccessed/><D:iscollection/><D:isstructureddocument/><D:defaultdocument/><D:isroot/></D:prop><D:status>HTTP/1.1 404 Not Found</D:status></D:propstat></D:response><D:response><D:href>/Welcome.docx</D:href><D:propstat><D:prop><D:displayname>Welcome.docx</D:displayname><D:getcontentlength>6396</D:getcontentlength><D:getlastmodified>Mon, 15 Jun 2009 18:03:20 GMT</D:getlastmodified><D:getcontenttype>application/msword-xml</D:getcontenttype><D:resourcetype/></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat><D:propstat><D:prop><D:name/><D:parentname/><D:href/><D:ishidden/><D:isreadonly/><D:contentclass/><D:getcontentlanguage/><D:creationdate/><D:lastaccessed/><D:iscollection/><D:isstructureddocument/><D:defaultdocument/><D:isroot/></D:prop><D:status>HTTP/1.1 404 Not Found</D:status></D:propstat></D:response></D:multistatus>');
        
        return dav instanceof XML
                && dav.length() == 1;
    },
    
    test_XML_as_function_with_string: function ()
    {
        var x = XML("text node");
        
        return x instanceof XML
                && x.nodeKind() === "text";
    },
    
    test_XML_as_function_with_xml: function ()
    {
        var x = XML("<xml/>");
        
        return x instanceof XML
                && x.nodeKind() === "element"
                && x.localName() === "xml";
    },
    
    test_namespace_zero_arguments: function ()
    {
        var ns = dav.namespace();
        return ns instanceof Namespace
                && ns.prefix === "D"
                && ns.uri === "DAV:";
    },
    
    test_namespace_one_argument: function ()
    {
        var nsi = dav.namespace("D");
        return nsi instanceof Namespace
                && nsi.prefix === "D"
                && nsi.uri === "DAV:";
    },
    
    test_addNamespace: function ()
    {
        var ns = new Namespace("PRE", "urn://sams-test");
        return dav.addNamespace(ns) === dav
                && dav.namespace("PRE") === ns;
    },
    
    test_addNamespace_from_uri: function ()
    {
        dav.addNamespace("urn://sams-test-addition");
        var names = dav.namespaceDeclarations();
        return names[names.length-1].uri === "urn://sams-test-addition";
    },
    
    test_appendChild: function ()
    {
        return dav.appendChild(new XML("test text node")).children().length() == 5;
    },
    
    test_attribute: function ()
    {
        var a = dav.attribute("att");
        return a instanceof XMLList
                && a.nodeKind() == "attribute"
                && a.toString() == "test";
    },
    
    test_attributes: function ()
    {
        var a = dav.attributes();
        return a instanceof XMLList
                && a.length() == 1;
    },
    
    test_child_numbered_index: function ()
    {
        var a = dav.child(0);
        return a instanceof XML
                && a.length() == 1
                && a.namespace().uri == dav.namespace().uri
                && a.localName() == "response";
    },
    
    test_child_named: function ()
    {
        var a = dav.child(QName(dav.namespace(), "response"));
        return a instanceof XMLList
                && a.length() == 4
                && a[0].namespace().uri == dav.namespace().uri
                && a[0].localName() == "response";
    },
    
    test_childIndex: function ()
    {
        var a = dav.child(1);
        return a instanceof XML
                && a[0].childIndex() == 1;
    },
    
    test_children: function ()
    {
        var children = dav.children();
        return children instanceof XMLList
                && children.length() == 5;
    },
    
    test_comments_without_ignore: function ()
    {
        XML.ignoreComments = false;
        var c = (new XML("<test><!-- comment --></test>")).comments();
        return c instanceof XMLList
                && c.length() == 1;
    },
    
    test_comments_with_ignore: function ()
    {
        XML.ignoreComments = true;
        var c = (new XML("<test><!-- comment --></test>")).comments();
        return c instanceof XMLList
                && c.length() == 0;
    },
    
    test_contains_XML: function ()
    {
        return dav.contains(dav);
    },
    
    test_contains_string_in_text: function ()
    {
        return (new XML("test")).contains("test");
    },
    
    test_contains_string_in_XML_element: function ()
    {
        return (new XML("<x>test</x>")).contains("test");
    },
    
    test_contains_string_in_XML_attribute: function ()
    {
        return dav.attribute("att").contains("test");
    },
    
    test_copy_simple: function ()
    {
        var x = new XML("<xml/>"),
            z = x.copy();
        return z instanceof XML
                && z.namespace().uri === x.namespace().uri
                && z.name() == x.name();
    },
    
    test_copy_complex: function ()
    {
        var z = dav.copy();
        return z instanceof XML
                && z.namespace().uri === dav.namespace().uri
                && z.name() == dav.name()
                && z.children().length() === dav.children().length();
    },
    
    test_descendants_no_arguments: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child></root>"),
            z = x.descendants();
            
        return z instanceof XMLList
                && z.length() == 4;
    },
    
    test_descendants_string_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child></root>"),
            z = x.descendants("grandchild");
            
        return z instanceof XMLList
                && z.length() == 2;
    },
    
    test_descendants_QName_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child></root>"),
            z = x.descendants(QName(x.namespace(), "grandchild"));
            
        return z instanceof XMLList
                && z.length() == 2;
    },
    
    test_elements_no_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child><tester/></root>"),
            z = x.elements();
            
        return z instanceof XMLList
                && z.length() == 3;
    },
    
    test_elements_string_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child><tester/></root>"),
            z = x.elements("child");
            
        return z instanceof XMLList
                && z.length() == 2;
    },
    
    test_elements_QName_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child><tester/></root>"),
            z = x.elements(QName(x.namespace(), "child"));
            
        return z instanceof XMLList
                && z.length() == 2;
    },
    
    test_elements_string_argument: function ()
    {
        var x = new XML("<root><child><grandchild/></child><child><grandchild/></child><tester/></root>"),
            z = x.elements("child");
            
        return z instanceof XMLList
                && z.length() == 2;
    },
    
    test_hasOwnProperty_element_name_string: function ()
    {
        return !dav.hasOwnProperty("response");
    },
    
    test_hasOwnProperty_element_name_QName: function ()
    {
        return dav.hasOwnProperty(QName(dav.namespace(), "response"));
    },
    
    test_hasOwnProperty_attribute_name_string: function ()
    {
        return dav.hasOwnProperty("@att");
    },
    
    test_hasOwnProperty_attribute_name_AttributeName_specific_namespace: function ()
    {
        return dav.hasOwnProperty(new AttributeName(QName(dav.attributes().namespace(), "att")));
    },
    
    test_hasOwnProperty_attribute_name_AttributeName_any_namespace: function ()
    {
        return !dav.hasOwnProperty(new AttributeName(QName("*", "att")));
    },
    
    test_hasOwnProperty_attribute_name_AttributeName_null_namespace: function ()
    {
        return !dav.hasOwnProperty(new AttributeName(QName("att")));
    },
    
    test_hasOwnProperty_prototype_property: function ()
    {
        return !dav.hasOwnProperty("hasOwnProperty");
    },
    
    test_hasComplexContent: function ()
    {
        return dav.hasComplexContent();
    },
    
    test_hasComplexContent_text: function ()
    {
        return !(new XML()).hasComplexContent();
    },
    
    test_hasComplexContent_attribute: function ()
    {
        return !dav.attribute("att").hasComplexContent();
    },
    
    test_hasSimpleContent: function ()
    {
        return !dav.hasSimpleContent();
    },
    
    test_hasSimpleContent_text: function ()
    {
        return (new XML()).hasSimpleContent();
    },
    
    test_hasSimpleContent_attribute: function ()
    {
        return dav.attribute("att").hasSimpleContent();
    },
    
    test_inScopeNamespaces_default_namespace: function ()
    {
        var x = new XML('<root/>'),
            n = x.inScopeNamespaces();
        
        return n.length == 1
                && n[0].uri == "urn://string-set-default-namespace-attr";
    },
    
    test_inScopeNamespaces_multiple_declared_namespaces: function ()
    {
        var x = new XML('<root><test xmlns="uri:newns"><web xmlns="uri:reader" xmlns:T="uri:newT"/></test></root>'),
            n = x.descendants()[1].inScopeNamespaces(),
            b = "|" + n.join("|") + "|";
        
        return n.length == 2
                && b.indexOf("|uri:reader|") > -1
                && b.indexOf("|uri:newT|") > -1;
    },
    
    test_inScopeNamespaces_multiple_declared_namespaces_different_prefixes: function ()
    {
        var x = new XML('<root><test xmlns:L="uri:newns"><web xmlns:N="uri:reader" xmlns:T="uri:newT"/></test></root>'),
            n = x.descendants()[1].inScopeNamespaces(),
            b = "|" + n.join("|") + "|";
        
        return n.length == 4
                && b.indexOf("|uri:reader|") > -1
                && b.indexOf("|uri:newT|") > -1
                && b.indexOf("|uri:newns|") > -1
                && b.indexOf("|urn://string-set-default-namespace-attr|") > -1;
    },
    
    test_insertChildAfter: function ()
    {
        var x = new XML('<root><first/></root>'),
            r = new XML('<second/>');
        
        return x.insertChildAfter(x.elements()[0], r) === x
                && x.elements().length() === 2
                && x.elements()[1] === r
                && x.elements('second').length() == 1;
    },
    
    test_insertChildBefore: function ()
    {
        var x = new XML('<root><first/></root>'),
            r = new XML('<second/>');
        
        return x.insertChildBefore(x.elements()[0], r) === x
                && x.elements().length() === 2
                && x.elements()[0] === r
                && x.elements('second').length() == 1;
    },
    
    test_length: function ()
    {
        return new XML().length() === 1;    
    },
    
    test_length_with_multiple_children: function ()
    {
        return new XML('<root><first/><second/><third/></root>').length() === 1;    
    },
    
    test_localName_element: function ()
    {
        return new XML('<second/>').localName() === "second";
    },
    
    test_localName_text: function ()
    {
        return new XML().localName() == null;
    },
    
    test_localName_with_namespace: function ()
    {
        return new XML('<r:test xmlns:r="uri:tester"/>').localName() === "test";
    },
    
    test_name: function ()
    {
        var x = new XML('<r:test xmlns:r="uri:tester"/>');
        return x.name().uri == "uri:tester"
                && x.name().localName === "test";
    },
    
    test_namespace_no_arguments: function ()
    {
        var x = new XML('<root xmlns="uri:newns"/>');
        
        return x.namespace() != null
                && x.namespace().uri === "uri:newns";
    },
    
    test_namespace_one_argument: function ()
    {
        var x = new XML('<root xmlns="uri:newns"/>');
        
        return x.namespace() != null
                && x.namespace().uri === "uri:newns";
    },
    
    test_namespace_one_argument_child: function ()
    {
        var x = new XML('<root xmlns="uri:newns"><web xmlns:N="uri:reader" xmlns:T="uri:newT"/></root>').elements()[0];
        
        return x.namespace() != null
                && x.namespace().uri === "uri:newns";
    },
    
    test_namespace_one_argument_child_different_namespace: function ()
    {
        var x = new XML('<root xmlns="uri:newns"><web xmlns="uri:reader" xmlns:T="uri:newT"/></root>').elements()[0];
        
        return x.namespace() != null
                && x.namespace().uri === "uri:reader";
    },
    
    test_namespaceDeclarations: function ()
    {
        return new XML('<root/>').namespaceDeclarations().length === 0;    
    },
    
    test_namespaceDeclarations_double: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT"><web xmlns:R="uri:reader" xmlns:T="uri:newT"/></root>').elements()[0],
            h = x.namespaceDeclarations(),
            b = "|" + h.join("|") + "|";
        
        return h.length === 2
                && b.indexOf("|uri:newns|") == -1;    
    },
    
    test_namespaceDeclarations_single: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT"><web xmlns="uri:reader" xmlns:T="uri:newT"/></root>').elements()[0],
            h = x.namespaceDeclarations();
        
        return h.length === 1
                && h[0].uri === "uri:newT";    
    },
    
    test_namespaceDeclarations_single_root: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT"><web xmlns="uri:reader" xmlns:T="uri:newT"/></root>'),
            h = x.namespaceDeclarations();
        
        return h.length === 2
                && h[0].uri === "uri:newns"
                && h[1].uri === "uri:newT";    
    },
    
    test_namespaceDeclarations_single_child: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT"><web xmlns="uri:reader" xmlns:T="uri:newT"><web xmlns="uri:newTG"/></web></root>'),
            h = x.descendants()[1].namespaceDeclarations();
        
        return h.length === 0;    
    },
    
    test_namespaceDeclarations_double: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT"><web xmlns:R="uri:reader" xmlns:T="uri:newT"/></root>').elements()[0],
            h = x.namespaceDeclarations(),
            b = "|" + h.join("|") + "|";
        
        return h.length === 2
                && b.indexOf("|uri:newns|") == -1;    
    },
    
    test_nodeKind_text: function ()
    {
        return new XML().nodeKind() === "text";
    },
    
    test_nodeKind_element: function ()
    {
        return new XML('<root/>').nodeKind() === "element";
    },
    
    test_nodeKind_comment: function ()
    {
        XML.ignoreComments = false;
        var x = new XML('<root><!-- comment --></root>').children()[0].nodeKind() === "comment";;
        XML.ignoreComments = true;
        return x;
    },
    
    test_nodeKind_processing_instruction: function ()
    {
        XML.ignoreProcessingInstructions = false;
        var x = new XML('<root><?xml-stylesheet type="text/css" href="/path" ?></root>').children()[0].nodeKind() === "processing-instruction";
        XML.ignoreProcessingInstructions = true;
        return x;
    },
    
    test_nodeKind_attribute: function ()
    {
        return new XML('<root att="test"/>').attribute("att").nodeKind() === "attribute";
    },
    
    test_normalize: function ()
    {
    return true;    
    },
    
    test_parent: function ()
    {
        var x = new XML('<root><first/><second/><third/></root>')
            l = x.children()[0];
        
        return l.parent() === x;
    },
    
    test_processingInstructions: function ()
    {
        XML.ignoreProcessingInstructions = false;
        return new XML('<root><?xml-stylesheet type="text/css" href="/path" ?></root>').processingInstructions().length() === 1;
    },
    
    test_processingInstructions_with_name: function ()
    {
        var x = new XML('<root><?xml-stylesheet type="text/css" href="/path" ?></root>').processingInstructions('xml-stylesheet').length() === 1;
        return x;
    },
    
    test_prependChild: function ()
    {
        var x = new XML('<root><first/></root>'),
            r = new XML('<second/>');
        
        return x.prependChild(r) === x
                && x.elements().length() === 2
                && x.elements()[0] === r
                && x.elements('second').length() == 1;    
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_propertyIsEnumerable: function ()
    {
        return new XML().propertyIsEnumerable('children') === false;    
    },
    
    test_propertyIsEnumerable_child: function ()
    {
        return new XML('<root><test/></root>').propertyIsEnumerable('test') === false;    
    },
    
    test_propertyIsEnumerable_0: function ()
    {
        return new XML('<root><test/></root>').propertyIsEnumerable(0);    
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_removeNamespace: function ()
    {
        var x = new XML('<root xmlns="uri:newns" xmlns:L="uri:newT" L:t="hype"><L:web t="typical" L:t="hype" xmlns:R="uri:reader" xmlns:T="uri:newT"/></root>');
        
        return x.namespaceDeclarations().length === 2;
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_replace: function ()
    {
        var x = new XML('<root><talk/><tag/></root>');
            x.replace(0, new XML('<silence/>'));
        
        return x.children()[0].localName() == 'silence'
                && x.children().length() == 2;
    },
    
    test_replace_list: function ()
    {
        var x = new XML('<root><talk/><talk/><tag/></root>');
            x.replace(1, new XML('<root><silence/><silence/></root>').elements('silence'));
        
        return x.elements('silence').length() === 2
                && x.elements('talk').length() == 1
                && x.elements('tag').length() == 1;
    },
    
    test_replace_list_namespace: function ()
    {
        var x = new XML('<root xmlns:R="tester"><talk/><talk/><R:tag/></root>'),
            v = x.elements(QName("tester", 'tag'));
            x.replace(1, new XML('<root><silence/><silence/></root>').elements('silence'));
        
        return x.elements('silence').length() === 2
                && x.elements('talk').length() == 1
                && x.elements(v.name()).length() == 1;
    },
    
    test_replace_list_namespace_QName_as_argument: function ()
    {
        var x = new XML('<root xmlns:R="tester"><talk/><talk/><R:tag/></root>'),
            v = x.elements(QName("tester", 'tag'));
            x.replace(x.children()[0].name(), new XML('<root><silence/><silence/></root>').elements('silence'));
        
        return x.elements('silence').length() === 2
                && x.elements('talk').length() == 0
                && x.elements(v.name()).length() == 0;
    },
    
    test_replace_string_as_argument: function ()
    {
        var x = new XML('<root><talk/><tag/></root>');
            x.replace("test", new XML('<silence/>'));
        
        return x.children().length() == 2
                && x.children()[0].localName() == 'talk';
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_setChildren: function ()
    {
        var x = new XML('<root xmlns:R="tester"><talk/><talk/><R:tag/></root>'),
            v = x.elements(QName("tester", 'tag'));
            x.setChildren(new XML('<root><silence/><silence/></root>').elements('silence'));
        
        return x.elements('silence').length() === 2
                && x.elements('talk').length() == 0
                && x.elements(v.name()).length() == 0;
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_setLocalName: function ()
    {
        var x = new XML('<root xmlns:f="test"><f:web/></root>');
            x.elements()[0].setLocalName("righteous");
        
        return x.elements()[0].localName() == "righteous";
    },
    
    test_setName_element: function ()
    {
        var x = new XML('<root xmlns:f="test"><f:web/></root>');
            x.elements()[0].setName("righteous");
        
        return x.elements()[0].name().localName == "righteous";    
    },
    
    test_setName_attribute: function ()
    {
        var x = new XML('<root xmlns:f="test" att="value"><f:web/></root>');
            x.attributes()[0].setName("righteous");
        
        return x.attribute(QName(null, "*"))[0].name().localName == "righteous";    
    },
    
    test_setName_text: function ()
    {
        var x = new XML('<root xmlns:f="test"> text </root>');
            x.text()[0].setName("righteous");
        
        return x.text().length() == 1
                && x.text()[0].localName() == null;    
    },
    
    test_setName_comment: function ()
    {
        XML.ignoreComments = false
        var x = new XML('<root xmlns:f="test"><!-- comment --></root>');
            x.comments()[0].setName("righteous");
        
        return x.comments().length() == 1
                && x.comments()[0].localName() == null;    
    },
    
    test_setName_processing_instruction: function ()
    {
        XML.ignoreProcessingInstructions = false;
        var x = new XML('<root xmlns:f="test"><?special data="test" ?></root>');
            x.processingInstructions()[0].setName("righteous");
        
        return x.processingInstructions().length() == 1
                && x.processingInstructions()[0].name().localName == "righteous";    
    },
    
    test_setName_element_QName: function ()
    {
        var x = new XML('<root xmlns:f="test"><f:web/></root>');
            x.elements()[0].setName(QName("righteous"));
        
        return x.elements()[0].name().localName == "righteous";    
    },
    
    test_setName_attribute_QName: function ()
    {
        var x = new XML('<root xmlns:f="test" att="test"><f:web/></root>')
            g = x.attributes()[0];
            g.setName(QName("righteous"));
        
        return x.attribute(g.name()).length() == 1
                && g.name().localName == "righteous";    
    },
    
    test_setName_text_QName: function ()
    {
        var x = new XML('<root xmlns:f="test"> text </root>');
            x.text()[0].setName(QName("righteous"));
        
        return x.text().length() == 1
                && x.text()[0].localName() == null;    
    },
    
    test_setName_comment_QName: function ()
    {
        XML.ignoreComments = false;
        var x = new XML('<root xmlns:f="test"><!-- comment --></root>');
            x.comments()[0].setName(QName("righteous"));
        
        return x.comments().length() == 1
                && x.comments()[0].localName() == null;    
    },
    
    test_setName_processing_instruction_QName: function ()
    {
        XML.ignoreProcessingInstructions = false;
        var x = new XML('<root xmlns:f="test"><?special data="test" ?></root>');
            x.processingInstructions()[0].setName(QName("righteous"));
        
        return x.processingInstructions().length() == 1
                && x.processingInstructions()[0].name().localName == "righteous";    
    },
    
    //need to write tests for text, comment, processing-instruction, and attribute
    
    test_setNamespace: function ()
    {
        var x = new XML('<root xmlns:f="test"><web/></root>');
            x.children()[0].setNamespace(x.namespaceDeclarations()[0]);
        
        return x.children()[0].name().localName == "web"
                && x.children()[0].name().uri == "test";    
    },
    
    test_setNamespace_attribute: function ()
    {
        var x = new XML('<root xmlns:f="test" att="value"><web/></root>'),
            g = x.attributes()[0];
            g.setNamespace(x.namespaceDeclarations()[0]);
        
        return x.attribute(g.name()).length() == 1
                && g.name().localName == "att"
                && g.name().uri == "test";    
    },
    
    test_setNamespace_text: function ()
    {
        var x = new XML('<root xmlns:f="test"> something texty </root>');
            //should fail
            x.text()[0].setNamespace(x.namespaceDeclarations()[0]);
        
        return x.text()[0].namespace() != x.namespaceDeclarations()[0];        
    },
    
    test_setNamespace_comment: function ()
    {
        XML.ignoreComments = false;
        var x = new XML('<root xmlns:f="test"><!-- comment --></root>');
            //should fail
            x.comments()[0].setNamespace(x.namespaceDeclarations()[0]);
        
        return x.comments()[0].namespace() != x.namespaceDeclarations()[0];    
    },
    
    test_setNamespace_processing_instruction: function ()
    {
        XML.ignoreProcessingInstructions = false;
        var x = new XML('<root xmlns:f="test"><?special data="test" ?></root>');
            x.processingInstructions()[0].setNamespace(x.namespaceDeclarations()[0]);
        
        return x.processingInstructions()[0].name().localName == "special"
                && x.processingInstructions()[0].name().uri == "test";    
    },
    
    test_text: function ()
    {
        return new XML('<root> comment </root>').text()[0].toString()  == ' comment ';
    },
    
    test_toString_element: function ()
    {
        XML.prettyPrinting = false;
        var x = new XML('<root xmlns:f="test"><f:web/></root>');
        return x.toString() == '<root xmlns:f="test" xmlns="'+x.namespace().uri+'"><f:web/></root>';    
    },
    
    test_toString_attribute: function ()
    {
        return new XML('<root x="value"/>').attribute("x")[0].toString()  == 'value';
    },
    
    test_toString_comment: function ()
    {
        XML.ignoreComments = false;
        return new XML('<root><!-- comment --></root>').comments()[0].toString()  == '<!-- comment -->';
    },
    
    test_toString_Processing_Instructions: function ()
    {
        XML.ignoreProcessingInstructions = false;
        return new XML('<root><?special data="test" ?></root>').processingInstructions()[0].toString()  == '<?special data="test" ?>';
    },
    
    test_toString_text: function ()
    {
        return new XML('<root> comment </root>').text()[0].toString()  == ' comment ';
    },
    
    test_toXMLString_element: function ()
    {
        XML.prettyPrinting = false;
        var x = new XML('<root xmlns:f="test"><f:web/></root>');
        return x.toXMLString() == '<root xmlns:f="test" xmlns="'+x.namespace().uri+'"><f:web/></root>';    
    },
    
    test_toXMLString_attribute: function ()
    {
        return new XML('<root x="value"/>').attribute("x")[0].toXMLString(true)  == 'value';
    },
    
    test_toXMLString_comment: function ()
    {
        XML.ignoreComments = false;
        return new XML('<root><!-- comment --></root>').comments()[0].toXMLString()  == '<!-- comment -->';
    },
    
    test_toXMLString_Processing_Instructions: function ()
    {
        XML.ignoreProcessingInstructions = false;
        return new XML('<root><?special data="test" ?></root>').processingInstructions()[0].toXMLString()  == '<?special data="test" ?>';
    },
    
    test_toXMLString_text: function ()
    {
        return new XML('<root> comment </root>').text()[0].toXMLString()  == ' comment ';
    },
    
    test_valueOf: function ()
    {
        var x = new XML();
        return x.valueOf() === x;
    },
    
    /**
     *    XMLList tests
     *
     *
     */
    test_XMLList: function ()
    {
        list = new XMLList("<talk/><talk/><talk/><talk/>");
        
        return list instanceof XMLList 
                && list.length() == 4;
    },
    
    test_XMLList_instanceof_XML: function ()
    {
        return list instanceof XML;
    },
    
    test_XMLList_has_default_namespace: function ()
    {
        return list[0].namespace().uri == "urn://string-set-default-namespace-attr";
    }
    
};

function count (obj)
{
    if ("__count__" in obj)
    {
        return obj.__count__;
    }
    
    var i = 0, k;
    
    for (k in obj)
    {
        if (obj.hasOwnProperty(k))
            ++i;
    }
    
    return i;
}

test_run(tests);
})();


